package com.queen.restaurant.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.queen.restaurant.domain.Restaurant;
import com.queen.restaurant.domain.Table;
import com.queen.restaurant.service.TableService;

/**
 * 餐桌操作Controller
 * 
 * @author Queen
 * Description:
 * <br/>Program Name:QueenRestaurant
 * <br/>Date:2013.07.11
 * */

@Controller
public class TableController {

	@Autowired
	private TableService tableService;

	// 获取餐桌页面
	@RequestMapping(value = "/restaurant/table")
	public String getTablePage() {
		return "restaurant/restaurant_table";
	}

	// 登陆餐厅的所有餐桌
	@RequestMapping(value = "/restaurant/table/list")
	@ResponseBody
	public List<Table> getTableList(HttpSession session) {
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		List<Table> tableList = tableService.getTablesByRestaurantId(
				restaurant.getId());
		return tableList;
	}
	
	// 新增餐桌
	@RequestMapping(value = "/restaurant/table/create", method = RequestMethod.POST)
	@ResponseBody
	public List<Table> createTable(
			@RequestBody Table table,
			HttpSession session){
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		System.out.println("isroom=" + table.getIsRoom());
		table.setRestaurant(restaurant);
		table.setTableStatus(0);
		tableService.createTable(table);
		return tableService.getTablesByRestaurantId(restaurant.getId());
	}
	
	// 删除餐桌
	@RequestMapping(value = "/restaurant/table/delete", method = RequestMethod.POST)
	@ResponseBody
	public List<Table> deleteTable(
			@RequestBody String tableId,
			HttpSession session){
		tableService.deleteTableById(Integer.parseInt(tableId));
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		return tableService.getTablesByRestaurantId(restaurant.getId());
	}
	
	// 修改餐桌
	@RequestMapping(value = "/restaurant/table/update", method = RequestMethod.POST)
	@ResponseBody
	public List<Table> updateTable(
			@RequestBody Table table,
			HttpSession session){
		tableService.updateTable(table);
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		return tableService.getTablesByRestaurantId(restaurant.getId());
	}
	
	// 扫描餐桌ID获取餐桌
	@RequestMapping(value = "/table/get_table", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getTableById(
			@RequestParam("tableId") String tableId
			){
		Table table = null;
		int result;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		table = tableService.getTableById(tableId);
		result = table.getTableStatus();
		String restaurantName = table.getRestaurant().getRestaurantName();
		String tableName = table.getTableName();
		if (result != Table.TABLE_STATUS_FREE) {
			table = null;
		}
		resultMap.put("result", result);
//		resultMap.put("table", table);
		resultMap.put("tableName", tableName);
		resultMap.put("restaurantName", restaurantName);
		return resultMap;
	}
	
	// 更改餐桌状态
	@RequestMapping(value = "/table/set_table_status", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> setTableStatus(
			@RequestParam("tableId") String tableId,
			@RequestParam("tableStatus") int tableStatus
			){
		Table table = null;
		boolean result = false;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			table = tableService.getTableById(tableId);
			table.setTableStatus(tableStatus);
			tableService.updateTable(table);
			result = true;
		} catch (Exception e) {
			result = false;
		}
		
		resultMap.put("result", result);
		return resultMap;
	}
}
